package com.yqg.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yqg.manage.common.Result;
import com.yqg.manage.entity.Laws;
import com.yqg.manage.entity.Term;
import com.yqg.manage.mapper.TermMapper;
import com.yqg.manage.service.ILawsService;
import com.yqg.manage.service.ITermService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * TermServiceImpl 服务实现类
 *
 * @author zxf
 * @since 2025-07-26
 */
@Service
public class TermServiceImpl extends ServiceImpl<TermMapper, Term> implements ITermService {
    @Resource
    private ILawsService lawsService;
    @Resource
    private TermMapper termMapper;

    @Override
    public Map<String, Object> getLevelData(Integer level, Long lowsId, String article, String paragraph, String subparagraph, String item) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", null);
        map.put("content", null);
        if (level == 1) {
            // 获取条
            LambdaQueryWrapper<Term> termLambdaQueryWrapper = new LambdaQueryWrapper<>();
            termLambdaQueryWrapper.eq(Term::getLawsId, lowsId)
                    .eq(Term::getIsDelete, "0");
            List<Term> list = list(termLambdaQueryWrapper);
            Set<String> dataSet = new HashSet<>();
            list.forEach(term -> {
                if (term.getArticle() != null) {
                    dataSet.add(term.getArticle());
                }
            });

            map.put("dataList", dataSet);

        } else if (level == 2) {
            // 获取款
            LambdaQueryWrapper<Term> termLambdaQueryWrapper = new LambdaQueryWrapper<>();
            termLambdaQueryWrapper.eq(Term::getLawsId, lowsId)
                    .eq(Term::getArticle, article)
                    .eq(Term::getIsDelete, "0");
            List<Term> list = list(termLambdaQueryWrapper);
            Set<String> dataSet = new HashSet<>();
            if (list.size() == 1) {
                list.forEach(term -> {
                    if (term.getParagraph() == null) {
                        map.put("id", term.getId());
                        map.put("content", term.getContent());
                    }
                });
            }
            list.forEach(term -> {
                if (term.getParagraph() != null) {
                    dataSet.add(term.getParagraph());
                }
            });
            map.put("dataList", dataSet);
        } else if (level == 3) {
            // 获取项
            LambdaQueryWrapper<Term> termLambdaQueryWrapper = new LambdaQueryWrapper<>();
            termLambdaQueryWrapper.eq(Term::getLawsId, lowsId)
                    .eq(Term::getArticle, article)
                    .eq(Term::getParagraph, paragraph)
                    .eq(Term::getIsDelete, "0");
            List<Term> list = list(termLambdaQueryWrapper);
            Set<String> dataSet = new HashSet<>();
            if (list.size() == 1) {
                list.forEach(term -> {
                    if (term.getSubparagraph() == null) {
                        map.put("id", term.getId());
                        map.put("content", term.getContent());
                    }
                });
            }
            list.forEach(term -> {
                if (term.getSubparagraph() != null) {
                    dataSet.add(term.getSubparagraph());
                }
            });
            map.put("dataList", dataSet);

        } else {
            // 获取目
            LambdaQueryWrapper<Term> termLambdaQueryWrapper = new LambdaQueryWrapper<>();
            termLambdaQueryWrapper.eq(Term::getLawsId, lowsId)
                    .eq(Term::getArticle, article)
                    .eq(Term::getParagraph, paragraph)
                    .eq(Term::getSubparagraph, subparagraph)
                    .eq(Term::getIsDelete, "0");
            List<Term> list = list(termLambdaQueryWrapper);
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (list.size() == 1) {
                list.forEach(term -> {
                    if (term.getItem() == null) {
                        map.put("id", term.getId());
                        map.put("content", term.getContent());
                    }
                });
            }
            list.forEach(term -> {
                if (term.getItem() != null) {
                    Map<String, Object> dataMap = new HashMap<>();
                    dataMap.put("id", term.getId());
                    dataMap.put("item", term.getItem());
                    dataMap.put("content", term.getContent());
                    dataList.add(dataMap);
                }
            });

            map.put("dataList", dataList);
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(Term term) {
        if (term == null) {
            return false;
        }
//        如果法律不存在，新增法律
        if (term.getLawsId() == null) {
            Laws laws = new Laws();
            laws.setType(term.getLawType());
            laws.setName(term.getLawName());
            boolean b = lawsService.save(laws);
            if (b) {
                term.setLawsId(laws.getId());
            }
        }

        // 查询是否有重复数据
        LambdaQueryWrapper<Term> termWrapper = new LambdaQueryWrapper<>();
        termWrapper.eq(Term::getLawsId, term.getLawsId())
                .eq(Term::getArticle, term.getArticle())
                .eq(Term::getIsDelete, "0");
        if (term.getParagraph() != null) {
            termWrapper.eq(Term::getParagraph, term.getParagraph());
            if (term.getSubparagraph() != null) {
                termWrapper.eq(Term::getSubparagraph, term.getSubparagraph());
                if (term.getItem() != null) {
                    termWrapper.eq(Term::getItem, term.getItem());
                }
            }
        }

        boolean exists = this.count(termWrapper) > 0;
        if (exists && term.getId() == null) {
            // 记录已存在，不执行保存
            return null;
        } else {
            return this.saveOrUpdate(term);
        }
    }
}
